package com.huawei.app.demo.msg;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;

import com.alibaba.fastjson.JSONObject;
import com.huawei.app.demo.util.LogUtils;
import com.huawei.app.httpapi.MsgRspCallback;
import com.huawei.app.httpapi.ThreadPools;

/**
 * 会议结束通知
 *
 * @since 2021-01-29
 */
public class ConfEndMsgCallback implements MsgRspCallback {

    private static final String TAG = ConfEndMsgCallback.class.getSimpleName();
    private Context context;

    public ConfEndMsgCallback(Context context) {
        this.context = context;
    }

    @Override
    public int getMsgId() {
        return 4037;
    }

    @Override
    public String getMsgName() {
        return "MSG_END_CONF_RSP";
    }

    @Override
    public void handleMsg(JSONObject msgObj) {
        LogUtils.getInstance().i(TAG, "conf end msg is " + msgObj);

        // 会议结束时将当前应用拉到前台
        try {
            int taskId = ((Activity)context).getTaskId();
            ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);

            // 循环多次是避免系统将应用再次切换为后台任务
            int waitTimes = 0;
            while (waitTimes < 10) {
                activityManager.moveTaskToFront(taskId, ActivityManager.MOVE_TASK_WITH_HOME);
                LogUtils.getInstance().i(TAG, "moveTaskToFront times = " + waitTimes);
                ThreadPools.pause(500);
                waitTimes++;
            }
        } catch (Exception e) {
            LogUtils.getInstance().e(TAG, e.getMessage());
        }
    }
}
